/**
 * Created by Administrator on 2017/12/21.
 */
/**
 * 栏目管理初始化
 */
var Rec = {
    id: "RecTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Rec.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle'},
        {title: '推荐标题', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '推荐位置', field: 'locationName', align: 'center', valign: 'middle', sortable: true},
        {title: '产品选择', field: 'cateName', align: 'center', valign: 'middle', sortable: true},
        {title: '图片', field: 'picPath', align: 'center', valign: 'middle', sortable: true},
        {title: '排序值', field: 'order', align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
Rec.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Rec.seItem = selected[0];
        if(Rec.seItem == 'undefined' || Rec.seItem == '' || Rec.seItem == null) {
            Rec.seItem = selected[1];
        }
        return true;
    }
};
Rec.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Rec.seItem = selected;
        return true;
    }
};

/**
 * 点击添加推荐
 */
Rec.openAddRec = function () {
    var index = layer.open({
        type: 2,
        title: '推荐添加',
        area: ['900px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/rec/video/recAdd'
    });
    this.layerIndex = index;
};

/**
 * 打开查看推荐详情
 */
Rec.openRecDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '视频修改',
            area: ['900px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/rec/video/recUpdate/' + Rec.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Rec.formParams = function() {
    var queryData = {};

    queryData['keyword'] = $("#keyword").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    return queryData;
};

/**
 * 查询推荐列表
 */
Rec.search = function () {
    Rec.table.refresh({query: Rec.formParams()});
};

Rec.resetSearch = function () {
    $("#keyword").val("");
    $("#beginTime").val("");
    $("#endTime").val("");
    Rec.search();
}

/**
 * 删除推荐
 */
Rec.delete = function () {
    if (this.batchCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/rec/video/delete", function (data) {
                Feng.success("删除成功!");
                Rec.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = '';
            console.log(Rec.seItem);
            for (var i = 0; i < Rec.seItem.length;i++) {
                if(i < Rec.seItem.length - 1) {
                    ids += Rec.seItem[i].id+",";
                } else {
                    ids += Rec.seItem[i].id;
                }
            }
            ajax.set("recIds", ids);
            ajax.start();
        };

        var names = "";
        for (var i = 0; i < Rec.seItem.length;i++) {
            if(i < Rec.seItem.length - 1) {
                names += Rec.seItem[i].title+" ";
            } else {
                names += Rec.seItem[i].title;
            }
        }
        Feng.confirm("是否删除推荐 " + names + "?", operation);
    }
};

$(function () {
    var defaultColunms = Rec.initColumn();
    var table = new BSTable(Rec.id, "/rec/video/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Rec.formParams());
    Rec.table = table.init();
});

